
#include <assert.h>
#include "app_platform/cmd_obj.h"
#include "app_platform/cmd_obj_ext.h"
#include "app_platform/cntl_info.h"
#include "app_platform/intf_service_control.h"
#include "companydao_adaptor.h"
#include "companydao_req_resp.h"


std::string g_scompanydaoStandAlone;
#include "app_platform/res_factory.h"
#include "c2cplatform/component/dal/dal_set.h"

using namespace c2cplatform::component::dal;

///////////////////////
// class CCompanyDaoSvcFactory
int CCompanyDaoSvcFactory::init(void* pvParam)
{
	IUserDefineCntl* pCntl = reinterpret_cast<IUserDefineCntl*>(pvParam);
	assert(pCntl);
		CDAL_SET->AddCmd("0xc0661803");
		CDAL_SET->AddCmd("0xc0661804");
		CDAL_SET->AddCmd("0xc0661802");
		CDAL_SET->AddCmd("0xc0661801");

	return CCompanyDao::Initialize(pCntl);
}

int CCompanyDaoSvcFactory::CXYZTimerHandler::OnTimer(int iTimerId)
{
    assert(0);
	return 0;
}

int CCompanyDaoSvcFactory::fini(void* pvParam)
{
    return 0;
}

uint32_t CCompanyDaoSvcFactory::getCateId() const
{   
	//方法已废弃
    return 0;
}

IService* CCompanyDaoSvcFactory::createService(uint32_t dwcmdId)
{
    return (new CCompanyDaoService(dwcmdId));
}

void CCompanyDaoSvcFactory::getServiceType(ServiceTypeMap_T& mapSvcType)
{
	mapSvcType[COMPANYDAO_ADDANDUPDATECOMPANY_REQ_CMD_ID] = true;
	mapSvcType[COMPANYDAO_DELETECOMPANY_REQ_CMD_ID] = true;
	mapSvcType[COMPANYDAO_GETCOMPANY_REQ_CMD_ID] = true;
	mapSvcType[COMPANYDAO_GETCOMPANYBYIDS_REQ_CMD_ID] = true;
}

////////////////////////
// class CCompanyDaoService
CCompanyDaoService::CCompanyDaoService(uint32_t dwcmdId) 
: m_dwcmdId(dwcmdId)
{
    m_pSvc = new CCompanyDao;
}

void CCompanyDaoService::Reset() 
{ 
	m_pSvc -> Reset();
}
    
void CCompanyDaoService::getCmdIdRange(uint32_t& dwcmdIdBegin, uint32_t& dwcmdIdEnd) const 
{ 
    dwcmdIdBegin = m_dwcmdId;
    dwcmdIdEnd = m_dwcmdId;
} 
	
int CCompanyDaoService::Execute(CCmdObj& oCmd)
{
	switch(oCmd.getCmdId())
	{
	case COMPANYDAO_ADDANDUPDATECOMPANY_REQ_CMD_ID:
        return AddAndUpdateCompany(oCmd);
	case COMPANYDAO_DELETECOMPANY_REQ_CMD_ID:
        return DeleteCompany(oCmd);
	case COMPANYDAO_GETCOMPANY_REQ_CMD_ID:
        return GetCompany(oCmd);
	case COMPANYDAO_GETCOMPANYBYIDS_REQ_CMD_ID:
        return GetCompanyByIds(oCmd);
	default:
		assert(0);
	}
	return 0;
}

int CCompanyDaoService::AddAndUpdateCompany(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CCompanyDaoAddAndUpdateCompanyReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CCompanyDaoAddAndUpdateCompanyResp oResp;
	
    oResp.dwResult = m_pSvc->AddAndUpdateCompany(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.companyInfo,
		oReq.inReserve,
		oReq.errmsg,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CCompanyDaoService::DeleteCompany(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CCompanyDaoDeleteCompanyReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CCompanyDaoDeleteCompanyResp oResp;
	
    oResp.dwResult = m_pSvc->DeleteCompany(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.companyInfo,
		oReq.inReserve,
		oReq.errmsg,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CCompanyDaoService::GetCompany(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CCompanyDaoGetCompanyReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CCompanyDaoGetCompanyResp oResp;
	
    oResp.dwResult = m_pSvc->GetCompany(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.companyFilter,
		oReq.inReserve,
		oReq.errmsg,
		oReq.companyInfo,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.companyInfo = oReq.companyInfo;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CCompanyDaoService::GetCompanyByIds(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CCompanyDaoGetCompanyByIdsReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	if (oCmd.getVersion() == 3)
    {
        CCmdObjExt& oCmdExt = dynamic_cast<CCmdObjExt&>(oCmd);
        oCntlInfo.setAppId(oCmdExt.getReserve1());
        oCntlInfo.setDomainId(oCmdExt.getReserve2());
    }
	
	CCompanyDaoGetCompanyByIdsResp oResp;
	
    oResp.dwResult = m_pSvc->GetCompanyByIds(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.companysysNoList,
		oReq.inReserve,
		oReq.errmsg,
		oReq.companyInfo,
		oReq.outReserve
    );
	oResp.errmsg = oReq.errmsg;
	oResp.companyInfo = oReq.companyInfo;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


#include "svc_main.h"

IServiceFactory* GetServiceFactory(void)
{
    return CSvcFactorySingleton::Instance();
}
